package com.raven.craft.code.generator.config;

import com.raven.craft.code.generator.IGenerator;
import com.raven.craft.code.generator.mysql.repo.mapper.ColumnMapper;
import com.raven.craft.code.generator.mysql.repo.mapper.TableMapper;
import com.raven.craft.code.generator.mysql.repo.mapper.TableStatusMapper;
import com.raven.craft.code.generator.mysql.mybatis.MyBatisProperties;
import com.raven.craft.code.generator.mysql.mybatis.MybatisGenerator;
import com.raven.craft.code.generator.mysql.schema.excel.ExcelSchemaGenerator;
import com.raven.craft.code.generator.mysql.schema.excel.ExcelSchemaProperties;
import com.raven.craft.code.generator.mysql.schema.md.MdSchemaGenerator;
import com.raven.craft.code.generator.mysql.schema.md.MdSchemaProperties;
import com.raven.craft.code.generator.mysql.schema.puml.PumlSchemaGenerator;
import com.raven.craft.code.generator.mysql.schema.puml.PumlSchemaProperties;
import com.raven.craft.code.generator.mysql.sql.SqlGenerator;
import com.raven.craft.code.generator.mysql.sql.SqlProperties;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author raven
 * @date 2019-06-23 12:43
 */
@Configuration
@ConditionalOnProperty(name = "generator.mysql.database")
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
@EnableConfigurationProperties({MysqlProperties.class, MdSchemaProperties.class,
        MyBatisProperties.class, PumlSchemaProperties.class, ExcelSchemaProperties.class,
        SqlProperties.class})
public class MysqlGeneratorAutoConfig {

    @Bean
    public MysqlContext mysqlContext(MysqlProperties mysqlProperties,
                                     TableMapper tableMapper, ColumnMapper columnMapper, TableStatusMapper tableStatusMapper) {
        return MysqlContext.create(mysqlProperties, tableMapper, columnMapper, tableStatusMapper);
    }

    @Bean
    @ConditionalOnProperty(prefix = "generator.mysql.schema.md", name = "enabled", havingValue = "true", matchIfMissing = true)
    public IGenerator markdownSchemaGenerator(MysqlContext context,
                                              MdSchemaProperties markdownSchemaProperties) {
        return new MdSchemaGenerator(context, markdownSchemaProperties);
    }

    @Bean
    @ConditionalOnProperty(prefix = "generator.mysql.schema.excel", name = "enabled", havingValue = "true", matchIfMissing = true)
    public IGenerator excelSchemaGenerator(MysqlContext context,
                                           ExcelSchemaProperties excelSchemaProperties) {
        return new ExcelSchemaGenerator(context, excelSchemaProperties);
    }

    @Bean
    @ConditionalOnProperty(prefix = "generator.mysql.schema.puml", name = "enabled", havingValue = "true", matchIfMissing = true)
    public IGenerator pumlGenerator(MysqlContext context,
                                    PumlSchemaProperties pumlProperties) {
        return new PumlSchemaGenerator(context, pumlProperties);
    }

    @Bean
    @ConditionalOnProperty(prefix = "generator.mysql.mybatis", name = "enabled", havingValue = "true", matchIfMissing = true)
    public IGenerator mybatisGenerator(MysqlContext context,
                                       MyBatisProperties myBatisProperties) {
        return new MybatisGenerator(context, myBatisProperties);
    }

    @Bean
    @ConditionalOnProperty(prefix = "generator.mysql.sql", name = "enabled", havingValue = "true", matchIfMissing = true)
    public IGenerator sqlGenerator(MysqlContext context,
                                   SqlProperties sqlProperties) {
        return new SqlGenerator(context, sqlProperties);
    }
}
